﻿using MyShop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典项目某某管理系统.Basic
{
    public partial class FrmType : Form
    {
        public FrmType()
        {
            InitializeComponent();
        }

        private void BindData()
        {
            DBHelper.PrepareSql("select * from [Type]");
            DataTable dt = DBHelper.ExecQuery();
            this.dataGridView1.DataSource = dt;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string TypeID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "update [Type] set TypeName = @TypeName where TypeID =@TypeID";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("TypeName", this.textBox1.Text);
            DBHelper.SetParameter("TypeID", TypeID);
            int count = DBHelper.ExecNonQuery();
            if (count > 0)
            {
                MessageBox.Show("更新成功");
                BindData();
            }
            else
            {
                MessageBox.Show("更新失败");
            }
        }

        private void FrmType_Load(object sender, EventArgs e)
        {
            BindData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "insert into [Type](TypeName) values(@TypeName)";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("TypeName", this.textBox1.Text);
            int count = DBHelper.ExecNonQuery();
            if (count > 0)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string TypeID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            string sql = "select TypeName from [Type] where TypeID=@TypeID";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("TypeID", TypeID);
            DataTable dt = DBHelper.ExecQuery();
            this.textBox1.Text = dt.Rows[0]["TypeName"].ToString();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗?", "提示框", MessageBoxButtons.YesNo);
            if (dr==DialogResult.Yes)
            {
                string TypeID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                string sql = "delete from [Type] where TypeID =@TypeID";
                DBHelper.PrepareSql(sql);
                DBHelper.SetParameter("TypeID", TypeID);
                int count = DBHelper.ExecNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("删除成功");
                    BindData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }


        }
    }
}
